<?php
declare (strict_types = 1);

namespace app\admin\model\content;

use think\Model;

/**
 * @mixin \think\Model
 */
class Article extends Model
{
    protected $pk = "article_id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        $getInfo = $this->where('article_id',$id)->find()->toArray();
        $getInfo['content'] = htmlspecialchars_decode($getInfo['content']);
        return $getInfo;
    }

    /**
     * 获取指定类型列表
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getCategoryById($cid){
        return $this->where('cid',$cid)->select()->toArray();
    }


    /**
     * 列表
     * @param $getData
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit,$where = []){
        $sql = $this->where($where);
        if(isset($getData['title']) && $getData['title']) $sql = $sql->whereLike('title','%'.$getData['title'].'%');
        if(isset($getData['article_category_id']) && $getData['article_category_id'] ) $sql = $sql->where('cid',$getData['article_category_id']);
        $count = $sql->count();
        $statusType = [0=>'显示',1=>'隐藏'];
        $isHot = [0=>'否',1=>'是'];
        $articleCategoryModel = new ArticleCategory();
        $typeList =$articleCategoryModel->getAllList([],'article_category_id,title');
        $data = $sql->page($page,$limit)->order('article_id','desc')->select()->each(function ($item)use($statusType,$typeList,$isHot){
            $item['status_name'] = $statusType[$item['is_status']];
            $item['cid_name'] = isset($typeList[$item['cid']]) ? $typeList[$item['cid']] : '';
            $item['hot_name'] = isset($isHot[$item['is_hot']]) ? $isHot[$item['is_hot']] : '';
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     *  添加数据
     * @param $addData
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData($addData){
        return $this->insertGetId([
            'title'=>$addData['title'],
            'cid'=>$addData['cid'],
            'author'=>$addData['author'],
            'image_input'=>$addData['image_input'],
            'synopsis'=>$addData['synopsis'],
            'visit'=>$addData['visit'],
            'sort'=>$addData['sort'],
            'url'=>'',
            'admin_id'=>session('admin_id'),
            'is_hot'=>$addData['is_hot'],
            'is_status'=>$addData['is_status'],
            'type'=>isset($addData['type']) ? $addData['type'] : 1,
            'content'=>htmlspecialchars($addData['content']),
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 修改
     * @param $saveData
     * @return static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editData($saveData){
        $updateData = [
            'title'=>$saveData['title'],
            'cid'=>$saveData['cid'],
            'author'=>$saveData['author'],
            'image_input'=>$saveData['image_input'],
            'synopsis'=>$saveData['synopsis'],
            'visit'=>$saveData['visit'],
            'sort'=>$saveData['sort'],
            'is_hot'=>$saveData['is_hot'],
            'is_status'=>$saveData['is_status'],
            'content'=>htmlspecialchars($saveData['content']),
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->where('article_id',$saveData['id'])->update($updateData);
    }

    /**
     * 删除
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function delData($id){
        $getInfo = $this->getInfoById($id);
        if(!$getInfo) return ['code'=>0,'msg'=>'删除成功'];
        $result = $this->where('article_id',$id)->delete();
        if($result) return ['code'=>0,'msg'=>'删除成功'];
        return ['code'=>1,'msg'=>'删除失败，请稍后重试'];
    }

}
